package image.core;
import org.apache.commons.math3.linear.Array2DRowRealMatrix;
import org.apache.commons.math3.linear.RealMatrix;

public class DtcProcess {
	/**
	 * DCT的第一步是将矩阵各数字-128
	 * DCT处理的矩阵都是8*8的
	 * 输入输出的举着都为double
	 */
	RealMatrix matrixT, matrixT_trspt;
	public DtcProcess() {
		double[][] T = new double[8][8];
		for(int i=0; i<8; i++)
			for(int j=0; j<8; j++) {
				if(i==0)
					T[i][j] = 1/Math.sqrt(8);
				else {
					T[i][j] = Math.cos(Math.PI*i*(2*j+1)/16)/2;
				}
			}
		matrixT = new Array2DRowRealMatrix(T);
		matrixT_trspt = matrixT.transpose();
	}
	
	/**
	 * 首先将矩阵各数字-128
	 * @param matrix
	 * @return
	 */
	public double[][] dct_8(double[][] matrix){
		RealMatrix m = new Array2DRowRealMatrix(matrix);
		m = (matrixT.multiply(m)).multiply(matrixT_trspt);
		return m.getData();
	}
	
	public double[][] idct_8(double[][] matrix){

		RealMatrix m = new Array2DRowRealMatrix(matrix);
		m = (matrixT_trspt.multiply(m)).multiply(matrixT);
		return m.getData();
	}

}
